//
//  CLRMFuncView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/31.
//

#import "CLRMFuncView.h"
#import "CLReadMenu.h"
@interface CLRMFuncView()

/// 目录
@property (nonatomic, strong) UIButton *catalogue;

/// 设置
@property (nonatomic, strong) UIButton *setting;

/// 日夜间切换 (Day and Night)
@property (nonatomic, strong) UIButton *dn;

@end
@implementation CLRMFuncView


- (void)addSubviews {
    [super addSubviews];
    
    self.backgroundColor = UIColor.clearColor;
    
    // 目录
    UIButton *catalogue = [UIButton buttonWithType:UIButtonTypeCustom];
    self.catalogue = catalogue;
    [catalogue setImage:[[UIImage imageNamed:@"bar_0"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [catalogue addTarget:self action:@selector(clickCatalogue) forControlEvents:UIControlEventTouchUpInside];
    catalogue.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:catalogue];
    
    // 日夜间
    UIButton *dn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.dn = dn;
    [dn setImage:[[UIImage imageNamed:@"bar_2"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [dn addTarget:self action:@selector(clickDN:) forControlEvents:UIControlEventTouchUpInside];
    dn.tintColor = CL_READ_COLOR_MENU_COLOR;
    dn.selected = [CLUserDefaults boolForKey:CL_READ_KEY_MODE_DAY_NIGHT];
    [self addSubview:dn];
    [self updateDNButton];
    
    // 设置
    UIButton *setting = [UIButton buttonWithType:UIButtonTypeCustom];
    self.setting = setting;
    [setting setImage:[[UIImage imageNamed:@"bar_1"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [setting addTarget:self action:@selector(clickSetting) forControlEvents:UIControlEventTouchUpInside];
    
    setting.tintColor = CL_READ_COLOR_MENU_COLOR;
    [self addSubview:setting];
}

/// 点击目录
- (void)clickCatalogue {
    [self.readMenu.delegate readMenuClickCatalogue:self.readMenu];
}

/// 点击日夜间
- (void)clickDN:(UIButton *)button {
    
    button.selected = !button.isSelected;
    
    // 切换日夜间
    self.readMenu.cover.alpha = (CGFloat)(button.isSelected);
    
    // 刷新显示
    [self updateDNButton];
    
    // 记录日夜间状态
    [CLUserDefaults setBool:button.isSelected forKey:CL_READ_KEY_MODE_DAY_NIGHT];
    
    [self.readMenu.delegate readMenuClickDayAndNight:self.readMenu];
}

/// 点击设置
- (void)clickSetting {
    
    [self.readMenu showTopView:NO completion:nil];
    
    [self.readMenu showBottomView:NO completion:nil];
    
    [self.readMenu showSettingView:YES completion:nil];
    
}

/// 刷新日夜间按钮显示状态
- (void)updateDNButton {
    
    if (self.dn.isSelected) {
        self.dn.tintColor = CL_READ_COLOR_MAIN;
    }else{
        self.dn.tintColor = CL_READ_COLOR_MENU_COLOR;
    }
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat wh = self.frame.size.height;
    
    self.catalogue.frame = CGRectMake(0, CL_SPACE_SA_3, wh, wh);
    
    self.dn.frame = CGRectMake((self.frame.size.width - wh)/2, CL_SPACE_SA_3, wh, wh);
    
    self.setting.frame = CGRectMake(self.frame.size.width - wh, CL_SPACE_SA_3, wh, wh);
    
//    catalogue.frame = CGRect(x: 0, y: DZM_SPACE_SA_3, width: wh, height: wh)
//
//    dn.frame = CGRect(x: (frame.size.width - wh)/2, y: DZM_SPACE_SA_3, width: wh, height: wh)
//
//    setting.frame = CGRect(x: frame.size.width - wh, y: DZM_SPACE_SA_3, width: wh, height: wh)
}

@end
